package com.qf.web;

import com.qf.bean.Users;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

/**
 * 2019/11/16
 * Administrator
 * springmvc1116
 * 面向对象面向君  不负代码不负卿
 */

//作用等价于servlet，但是不需要继承任何类
@Controller
@SessionAttributes("modelmameuage")
public class TestController {
    //处理请求的时候，就是自定义的普通方法
    //接受参数的方式1.HttpServletRequest  2.单个接受,要求:方法的参数名=页面传值的key值
    //3.对象接受:对象中的属性名=页面传值的key值
    //后台返回数据给前台的方式:1.使用HttpservletRequest 2.使用Model类 3.ModelMap类 4.ModelAndView
   //4.session存值 :（1）HttpSession  （2）使用注解session,一定要搭配modelMap实现，用户只需要将数据保存在modelMap中
    //将modelmap中的key保存在session注解中即可
@RequestMapping("/test1")
public ModelAndView a1(String uname, int uage, HttpSession session,ModelMap modelmap,HttpServletRequest request){
    System.out.println("a1()->modelandview");
    ModelAndView modelAndView = new ModelAndView();
    modelAndView.addObject("modelandviewname",uname);
    modelAndView.setViewName("success");
    session.setAttribute("sessname",uname);
    modelmap.addAttribute("modelmameuage",uage);
    ServletContext application = request.getServletContext();
    return modelAndView;
}

   /* @RequestMapping("/test1")
    public String a1(HttpServletRequest request, String uname, int uage,
                     @RequestParam("uname") String name, @RequestParam("uage") int age,
                     Users users, Model model, ModelMap map){
        System.out.println("a1被执行");

        System.out.println("uname="+request.getParameter("uname"));//测试request
        System.out.println("age="+request.getParameter("uage"));
        System.out.println("uage="+uage+",uname="+uname);//测试同名参数
        System.out.println("age="+age+",name="+name);//测试非同名参数
        System.out.println("users="+users);//测试同名属性的

        //存值
        request.setAttribute("reqname",uname);//request
        model.addAttribute("modelname",uname);//model
        map.addAttribute("modelmapname",uname);//modelmap
        return  "success";//return 要跳转的页面名称
    }*/

    //接受一组数据
    @RequestMapping("/test2")
    public String a2(String[] love,@RequestParam("love") List love1,Users users){
        System.out.println("a2()");
        for (String s : love) {
            System.out.println(s);
        }

        System.out.println("-------------------");
        for (Object o : love1) {
            System.out.println(o);
        }
        System.out.println("-------------------");
        List love2 = users.getLove();
        for (Object o : love2) {
            System.out.println(o);
        }
        return "success";
    }

    //测试日期接受,springmvc默认接受的日期格式：yyyy/MM/dd
    @RequestMapping("/test3")
    public String a3(String birthday, @DateTimeFormat(pattern = "yyyy-MM-dd") @RequestParam("birthday") Date birthday1, Users users){
        System.out.println(birthday);
        System.out.println(birthday1);
        System.out.println(users.getBirthday());
        System.out.println("test3()");
        return "success";
    }
}
